package cn.tedu.store.controller;

import cn.tedu.store.entity.*;
import cn.tedu.store.service.IProductService;
import cn.tedu.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    IProductService service;

    @GetMapping("/findProductByName")
    public JsonResult<List<Product>> findProductByName(String name, HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        return JsonResult.getSuccessJR(service.findProductByName(name));
    }

    @GetMapping("/findProductByCid/{cid}")
    public JsonResult<List<Product>> findProductByCid(@PathVariable  Integer cid,HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        List<Product> s= service.findProductByCid(cid);
        return JsonResult.getSuccessJR(service.findProductByCid(cid));
    }

    @GetMapping("/findAllCategory")
    public JsonResult<List<ProductCategory>> findAllCategory(HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        List<ProductCategory> te,p = service.findAllCategory();
        return JsonResult.getSuccessJR(service.findAllCategory());
    }

    @GetMapping("/favourite")
    public JsonResult<List<Product>> findFavourite(Integer uid, String username,HttpSession session,HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");

        List<Product> data = service.findFavourite(uid);
        return JsonResult.getSuccessJR(data);
    }

    @GetMapping("/{id}/get")
    public JsonResult<Product> findById(@PathVariable("id") Integer id, Integer uid,HttpSession session,HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        Product product=service.findById(id);
        // 生成访问记录
        // 如果用户已登录，记录访问信息
        if(uid!=null){
            UserVisitLog log=new UserVisitLog(null,
                    uid,
                    product.getId(),
                    product.getCategory().getId(),null);
            // 将访问记录添加到数据库中
            service.saveVisitLog(log);
        }
        return JsonResult.getSuccessJR(product);
    }

    @GetMapping("/hot")
    public JsonResult<List<Product>> getHotList(HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        // 查询
        List<Product> data = service.findHotList();
        // 返回
        return JsonResult.getSuccessJR(data);
    }

    /**
     * order-service服务远程访问
     * @param pid
     * @param num
     */
    @PostMapping("/increaseProductNum")
    public void increaseProductNum(@RequestBody Integer pid, @RequestParam(value = "num") Integer num,HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.increaseProductNum(pid, num);
    }

    /**
     * order-service服务远程访问
     * @param pid
     * @param num
     */
    @PostMapping("/reduceProductNum")
    public void reduceProductNum(@RequestBody Integer pid, @RequestParam(value = "num") Integer num,HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");

        service.reduceProductNum(pid, num);
    }

    /**
     * cart-service服务远程访问
     * @return
     */
    @GetMapping("/get/{id}")
    public JsonResult<Product> findById(@PathVariable Integer id,HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        return JsonResult.getSuccessJR(service.findById(id));
    }

    @PostMapping("/findById")
    public JsonResult<Product> findByPid( @RequestBody Integer pid,HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        return JsonResult.getSuccessJR(service.findById(pid));
    }
}
